package b.b.h;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.RectF;
import android.os.Build.VERSION;
import android.text.StaticLayout.Builder;
import android.text.TextDirectionHeuristic;
import android.text.TextDirectionHeuristics;
import android.text.TextPaint;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;
import androidx.appcompat.widget.AppCompatEditText;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;

/* compiled from: AppCompatTextViewAutoSizeHelper */
public class l {
    public static final RectF l = new RectF();
    public static ConcurrentHashMap<String, Method> m = new ConcurrentHashMap();
    public static ConcurrentHashMap<String, Field> n = new ConcurrentHashMap();
    public int a = 0;
    public boolean b = false;
    public float c = -1.0f;
    public float d = -1.0f;
    public float e = -1.0f;
    public int[] f = new int[0];
    public boolean g = false;
    public TextPaint h;
    public final TextView i;
    public final Context j;
    public final c k;

    /* compiled from: AppCompatTextViewAutoSizeHelper */
    public static class c {
        public void a(Builder builder, TextView textView) {
        }

        public boolean b(TextView textView) {
            return ((Boolean) l.e(textView, "getHorizontallyScrolling", Boolean.FALSE)).booleanValue();
        }
    }

    /* compiled from: AppCompatTextViewAutoSizeHelper */
    public static class a extends c {
        public void a(Builder builder, TextView textView) {
            builder.setTextDirection((TextDirectionHeuristic) l.e(textView, "getTextDirectionHeuristic", TextDirectionHeuristics.FIRSTSTRONG_LTR));
        }
    }

    /* compiled from: AppCompatTextViewAutoSizeHelper */
    public static class b extends a {
        public void a(Builder builder, TextView textView) {
            builder.setTextDirection(textView.getTextDirectionHeuristic());
        }

        public boolean b(TextView textView) {
            return textView.isHorizontallyScrollable();
        }
    }

    public l(TextView textView) {
        this.i = textView;
        this.j = textView.getContext();
        int i = VERSION.SDK_INT;
        if (i >= 29) {
            this.k = new b();
        } else if (i >= 23) {
            this.k = new a();
        } else {
            this.k = new c();
        }
    }

    public static Method d(String str) {
        try {
            Method method = (Method) m.get(str);
            if (method == null) {
                method = TextView.class.getDeclaredMethod(str, new Class[0]);
                if (method != null) {
                    method.setAccessible(true);
                    m.put(str, method);
                }
            }
            return method;
        } catch (Exception e) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Failed to retrieve TextView#");
            stringBuilder.append(str);
            stringBuilder.append("() method");
            Log.w("ACTVAutoSizeHelper", stringBuilder.toString(), e);
            return null;
        }
    }

    public static <T> T e(Object obj, String str, T t) {
        try {
            t = d(str).invoke(obj, new Object[0]);
            return t;
        } catch (Exception e) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Failed to invoke TextView#");
            stringBuilder.append(str);
            stringBuilder.append("() method");
            Log.w("ACTVAutoSizeHelper", stringBuilder.toString(), e);
            return t;
        }
    }

    public void a() {
        Object obj = (!i() || this.a == 0) ? null : 1;
        if (obj != null) {
            if (this.b) {
                if (this.i.getMeasuredHeight() > 0 && this.i.getMeasuredWidth() > 0) {
                    int i;
                    if (this.k.b(this.i)) {
                        i = 1048576;
                    } else {
                        i = (this.i.getMeasuredWidth() - this.i.getTotalPaddingLeft()) - this.i.getTotalPaddingRight();
                    }
                    int height = (this.i.getHeight() - this.i.getCompoundPaddingBottom()) - this.i.getCompoundPaddingTop();
                    if (i > 0 && height > 0) {
                        RectF rectF = l;
                        synchronized (rectF) {
                            rectF.setEmpty();
                            rectF.right = (float) i;
                            rectF.bottom = (float) height;
                            float c = (float) c(rectF);
                            if (c != this.i.getTextSize()) {
                                f(0, c);
                            }
                        }
                    }
                }
                return;
            }
            this.b = true;
        }
    }

    public final int[] b(int[] iArr) {
        if (r0 == 0) {
            return iArr;
        }
        Arrays.sort(iArr);
        ArrayList arrayList = new ArrayList();
        for (int i : iArr) {
            if (i > 0 && Collections.binarySearch(arrayList, Integer.valueOf(i)) < 0) {
                arrayList.add(Integer.valueOf(i));
            }
        }
        if (r0 == arrayList.size()) {
            return iArr;
        }
        int size = arrayList.size();
        int[] iArr2 = new int[size];
        for (int i2 = 0; i2 < size; i2++) {
            iArr2[i2] = ((Integer) arrayList.get(i2)).intValue();
        }
        return iArr2;
    }

    /* JADX WARNING: Removed duplicated region for block: B:39:0x0125  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x011a  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x011a  */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x0125  */
    /* JADX WARNING: Missing block: B:30:0x0104, code skipped:
            if (r7.getLineEnd(r7.getLineCount() - 1) != r8.length()) goto L_0x0115;
     */
    public final int c(android.graphics.RectF r21) {
        /*
        r20 = this;
        r0 = r20;
        r1 = r21;
        r2 = r0.f;
        r2 = r2.length;
        if (r2 == 0) goto L_0x0131;
    L_0x0009:
        r3 = 1;
        r2 = r2 - r3;
        r4 = 0;
        r5 = 1;
        r6 = 0;
    L_0x000e:
        if (r5 > r2) goto L_0x012c;
    L_0x0010:
        r6 = r5 + r2;
        r6 = r6 / 2;
        r7 = r0.f;
        r7 = r7[r6];
        r8 = r0.i;
        r8 = r8.getText();
        r9 = r0.i;
        r9 = r9.getTransformationMethod();
        if (r9 == 0) goto L_0x002f;
    L_0x0026:
        r10 = r0.i;
        r9 = r9.getTransformation(r8, r10);
        if (r9 == 0) goto L_0x002f;
    L_0x002e:
        r8 = r9;
    L_0x002f:
        r9 = android.os.Build.VERSION.SDK_INT;
        r10 = r0.i;
        r15 = r10.getMaxLines();
        r10 = r0.h;
        if (r10 != 0) goto L_0x0043;
    L_0x003b:
        r10 = new android.text.TextPaint;
        r10.<init>();
        r0.h = r10;
        goto L_0x0046;
    L_0x0043:
        r10.reset();
    L_0x0046:
        r10 = r0.h;
        r11 = r0.i;
        r11 = r11.getPaint();
        r10.set(r11);
        r10 = r0.h;
        r7 = (float) r7;
        r10.setTextSize(r7);
        r7 = r0.i;
        r10 = android.text.Layout.Alignment.ALIGN_NORMAL;
        r11 = "getLayoutAlignment";
        r7 = e(r7, r11, r10);
        r14 = r7;
        r14 = (android.text.Layout.Alignment) r14;
        r7 = r1.right;
        r13 = java.lang.Math.round(r7);
        r7 = 23;
        r12 = -1;
        if (r9 < r7) goto L_0x00cb;
    L_0x006f:
        r7 = r8.length();
        r9 = r0.h;
        r7 = android.text.StaticLayout.Builder.obtain(r8, r4, r7, r9, r13);
        r9 = r7.setAlignment(r14);
        r10 = r0.i;
        r10 = r10.getLineSpacingExtra();
        r11 = r0.i;
        r11 = r11.getLineSpacingMultiplier();
        r9 = r9.setLineSpacing(r10, r11);
        r10 = r0.i;
        r10 = r10.getIncludeFontPadding();
        r9 = r9.setIncludePad(r10);
        r10 = r0.i;
        r10 = r10.getBreakStrategy();
        r9 = r9.setBreakStrategy(r10);
        r10 = r0.i;
        r10 = r10.getHyphenationFrequency();
        r9 = r9.setHyphenationFrequency(r10);
        if (r15 != r12) goto L_0x00b1;
    L_0x00ad:
        r10 = 2147483647; // 0x7fffffff float:NaN double:1.060997895E-314;
        goto L_0x00b2;
    L_0x00b1:
        r10 = r15;
    L_0x00b2:
        r9.setMaxLines(r10);
        r9 = r0.k;	 Catch:{ ClassCastException -> 0x00bd }
        r10 = r0.i;	 Catch:{ ClassCastException -> 0x00bd }
        r9.a(r7, r10);	 Catch:{ ClassCastException -> 0x00bd }
        goto L_0x00c4;
    L_0x00bd:
        r9 = "ACTVAutoSizeHelper";
        r10 = "Failed to obtain TextDirectionHeuristic, auto size may be incorrect";
        android.util.Log.w(r9, r10);
    L_0x00c4:
        r7 = r7.build();
        r3 = r15;
        r4 = -1;
        goto L_0x00ee;
    L_0x00cb:
        r7 = r0.i;
        r7 = r7.getLineSpacingMultiplier();
        r9 = r0.i;
        r16 = r9.getLineSpacingExtra();
        r9 = r0.i;
        r17 = r9.getIncludeFontPadding();
        r9 = new android.text.StaticLayout;
        r11 = r0.h;
        r10 = r9;
        r18 = r11;
        r11 = r8;
        r4 = -1;
        r12 = r18;
        r3 = r15;
        r15 = r7;
        r10.<init>(r11, r12, r13, r14, r15, r16, r17);
        r7 = r9;
    L_0x00ee:
        if (r3 == r4) goto L_0x0109;
    L_0x00f0:
        r4 = r7.getLineCount();
        if (r4 > r3) goto L_0x0107;
    L_0x00f6:
        r3 = r7.getLineCount();
        r4 = 1;
        r3 = r3 - r4;
        r3 = r7.getLineEnd(r3);
        r8 = r8.length();
        if (r3 == r8) goto L_0x010a;
    L_0x0106:
        goto L_0x0115;
    L_0x0107:
        r4 = 1;
        goto L_0x0115;
    L_0x0109:
        r4 = 1;
    L_0x010a:
        r3 = r7.getHeight();
        r3 = (float) r3;
        r7 = r1.bottom;
        r3 = (r3 > r7 ? 1 : (r3 == r7 ? 0 : -1));
        if (r3 <= 0) goto L_0x0117;
    L_0x0115:
        r3 = 0;
        goto L_0x0118;
    L_0x0117:
        r3 = 1;
    L_0x0118:
        if (r3 == 0) goto L_0x0125;
    L_0x011a:
        r6 = r6 + 1;
        r3 = 1;
        r4 = 0;
        r19 = r6;
        r6 = r5;
        r5 = r19;
        goto L_0x000e;
    L_0x0125:
        r6 = r6 + -1;
        r2 = r6;
        r3 = 1;
        r4 = 0;
        goto L_0x000e;
    L_0x012c:
        r1 = r0.f;
        r1 = r1[r6];
        return r1;
    L_0x0131:
        r1 = new java.lang.IllegalStateException;
        r2 = "No available text sizes to choose from.";
        r1.<init>(r2);
        throw r1;
        */
        throw new UnsupportedOperationException("Method not decompiled: b.b.h.l.c(android.graphics.RectF):int");
    }

    public void f(int i, float f) {
        Resources system;
        Context context = this.j;
        if (context == null) {
            system = Resources.getSystem();
        } else {
            system = context.getResources();
        }
        float applyDimension = TypedValue.applyDimension(i, f, system.getDisplayMetrics());
        if (applyDimension != this.i.getPaint().getTextSize()) {
            this.i.getPaint().setTextSize(applyDimension);
            boolean isInLayout = this.i.isInLayout();
            if (this.i.getLayout() != null) {
                this.b = false;
                try {
                    Method d = d("nullLayouts");
                    if (d != null) {
                        d.invoke(this.i, new Object[0]);
                    }
                } catch (Exception e) {
                    Log.w("ACTVAutoSizeHelper", "Failed to invoke TextView#nullLayouts() method", e);
                }
                if (isInLayout) {
                    this.i.forceLayout();
                } else {
                    this.i.requestLayout();
                }
                this.i.invalidate();
            }
        }
    }

    public final boolean g() {
        int i = 0;
        if (i() && this.a == 1) {
            if (!this.g || this.f.length == 0) {
                int floor = ((int) Math.floor((double) ((this.e - this.d) / this.c))) + 1;
                int[] iArr = new int[floor];
                while (i < floor) {
                    iArr[i] = Math.round((((float) i) * this.c) + this.d);
                    i++;
                }
                this.f = b(iArr);
            }
            this.b = true;
        } else {
            this.b = false;
        }
        return this.b;
    }

    public final boolean h() {
        int[] iArr = this.f;
        int length = iArr.length;
        boolean z = length > 0;
        this.g = z;
        if (z) {
            this.a = 1;
            this.d = (float) iArr[0];
            this.e = (float) iArr[length - 1];
            this.c = -1.0f;
        }
        return z;
    }

    public final boolean i() {
        return (this.i instanceof AppCompatEditText) ^ 1;
    }

    public final void j(float f, float f2, float f3) throws IllegalArgumentException {
        String str = "px) is less or equal to (0px)";
        if (f <= 0.0f) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Minimum auto-size text size (");
            stringBuilder.append(f);
            stringBuilder.append(str);
            throw new IllegalArgumentException(stringBuilder.toString());
        } else if (f2 <= f) {
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append("Maximum auto-size text size (");
            stringBuilder2.append(f2);
            stringBuilder2.append("px) is less or equal to minimum auto-size text size (");
            stringBuilder2.append(f);
            stringBuilder2.append("px)");
            throw new IllegalArgumentException(stringBuilder2.toString());
        } else if (f3 > 0.0f) {
            this.a = 1;
            this.d = f;
            this.e = f2;
            this.c = f3;
            this.g = false;
        } else {
            StringBuilder stringBuilder3 = new StringBuilder();
            stringBuilder3.append("The auto-size step granularity (");
            stringBuilder3.append(f3);
            stringBuilder3.append(str);
            throw new IllegalArgumentException(stringBuilder3.toString());
        }
    }
}
